package com.hfjc.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hfjc.dao.IProductDao;
import com.hfjc.entity.Product;
import com.hfjc.enums.ResultEnum;
import com.hfjc.exception.ProductException;
import com.hfjc.service.IProductService;

@Service("ProductsService")
public class ProductService implements IProductService {

	@Autowired
	private IProductDao productDao;
	
	
	@Override
	public List<Product> findAll() {
		return productDao.findAll();
	}

	@Transactional
	@Override
	public Product productAdd(Product product) {
		return productDao.save(product);
	}

	@Override
	public Product findProductsById(Integer id) {
		return productDao.findOne(id);
	}

	@Transactional
	@Override
	public Product updateProductsById(Integer id,String productName,Float productPrice) {
		
		Product product =new Product();
		product.setProductPrice(productPrice);
		product.setProductName(productName);
		return productDao.save(product);
	}

	@Transactional
	@Override
	public String delProductsById(Integer id) {
		String result = null;
		try {
			productDao.delete(id);
			result="1";
		} catch (Exception e) {
			result="0";
		}
		
		return result;
	}

	@Override
	public List<Product> findProductsByPrice(Float price) {
		return productDao.findProductByProductPrice(price);
	}

	@Override
	public void findPriceById(Integer id) throws Exception{
		Product product = productDao.findOne(id);
		Float price = product.getProductPrice();
		if(price<10){
			throw new ProductException(ResultEnum.PRICE_0_10);
		}
		if(price>=10&&price<20){
			throw new ProductException(ResultEnum.PRICE_10_20);
		}
		
	}
	
}
